package tako.wisers.com.android.apis.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import javax.net.ssl.SSLHandshakeException;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;

public class HttpGeter {
	private HttpResponse mResponse;
	private HttpEntity mEntity;
	private ClientConnectionManager mConnectionManager;
	private static final int RELOAD_TIMES = 3;

	public HttpGeter(String url) {
		this(url, true);
	}

	public HttpGeter(String url, boolean reload) {
		HttpGet mGeter = new HttpGet(url);
		mGeter.getParams().setParameter("http.socket.timeout",
				new Integer(10000));
		DefaultHttpClient client = new DefaultHttpClient();
		HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
			@Override
			public boolean retryRequest(IOException exception,
					int executionCount, HttpContext context) {
				if (executionCount > RELOAD_TIMES) {
					return false;
				}

				if (exception instanceof NoHttpResponseException) {
					// 服务停掉则重新尝试连接
					return true;
				}
				if (exception instanceof SSLHandshakeException) {
					// SSL异常不需要重试
					return false;
				}
				return true;
			}

		};
		client.getParams().setParameter("http.socket.timeout",
				new Integer(10000));
		client.setHttpRequestRetryHandler(retryHandler);
		mConnectionManager = client.getConnectionManager();
		try {
			mResponse = client.execute(mGeter);
			mEntity = mResponse.getEntity();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int getResponseCode() {
		if (mResponse == null) {
			return -1;
		}
		return mResponse.getStatusLine().getStatusCode();
	}

	public String getContent() throws IOException {
		InputStream is = null;
		try {
			is = getInputStream();
			if(is == null) return null;
			return IOUtils.toString(is, "UTF-8");
		} finally {
			if (is != null) {
				try {
					is.close();
					close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public String getHeader(String key) {
		if (mResponse == null)
			return null;
		Header header = mResponse.getFirstHeader(key);
		String value = header != null ? header.getValue() : null;
		return value;
	}

	public InputStream getInputStream() {
		try {
			if (mEntity != null) {
				return mEntity.getContent();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void close() {
		mConnectionManager.closeExpiredConnections();
	}

}
